/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twodstar.selfie;


import java.io.File;

import com.facebook.widget.ProfilePictureView;
import com.parse.ParseUser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;


/**
 * Base class for a list element in the Scrumptious main display, consisting of an
 * icon to the left, and a two line display to the right.
 */
public class Element_BestList 
{
	private static final int CAMERA = 0;
    private static final int GALLERY = 1;
    
    public static final int REQUEST_CAMERA = 512;
    public static final int REQUEST_PHOTO = 513;
    
    private static final String FILE_PREFIX = "selfie_img_";
    private static final String FILE_SUFFIX = ".jpg";
    public static Element_BestList m_ToAttackObject = null;
    
    private static Uri tempUri = null;
    
    private Fragment m_HostFragment = null;
	private Activity m_Activity;
    private BaseAdapter adapter;
    
	private String m_FBID;
	private String m_Name;
	private String m_Title;
	private int m_Level;
	private int m_BattleTotal;
	private int m_BattleWin;
    
	private ProfilePictureView m_ProfilePic;
    private TextView m_TextView_Name;
    private TextView m_TextView_Title;
    private TextView m_TextView_Level;
    private TextView m_TextView_Battle;
    private Button m_BtnChallenge;
    private boolean m_IsToHideButtonChallenge = false;
    
   
    public Element_BestList(	Fragment HostFragment,
    							Activity PActivity, 
		    					String FBID,
								String Name,
								String Title,
								int Level,
								int BattleTotal,
								int BattleWin,
								boolean IsToHideButtonChallenge
    						) 
    {
    	m_Activity = PActivity;
    	m_HostFragment = HostFragment;
    	m_FBID = FBID;
    	m_Name = Name;
    	m_Title = Title;
    	m_Level = Level;
    	m_BattleTotal = BattleTotal;
    	m_BattleWin = BattleWin;
    	m_IsToHideButtonChallenge = IsToHideButtonChallenge;
    }
    
    public void SetViewElements( 	
    								
    								ProfilePictureView ProfilePic,
								    TextView TextView_Name,
								    TextView TextView_Title,
								    TextView TextView_Level,
								    TextView TextView_Battle,
								    Button BtnChallenge
								    )
    {
    	//Log.i("game", "SetViewElements-----------------");
    	
    	
    	m_ProfilePic = ProfilePic;
        m_TextView_Name = TextView_Name;
        m_TextView_Title = TextView_Title;
        m_TextView_Level = TextView_Level;
        m_TextView_Battle = TextView_Battle;
        m_BtnChallenge = BtnChallenge;
        
        if(m_ProfilePic != null)
        	m_ProfilePic.setProfileId(m_FBID);
        
        if(m_TextView_Name != null)
        	m_TextView_Name.setText(m_Name);
        
        if(m_TextView_Title != null)
        	m_TextView_Title.setText("Title: " + m_Title);
        
        if(m_TextView_Level != null)
        	m_TextView_Level.setText("Level: " + m_Level);
        
        if(m_TextView_Battle != null)
        	m_TextView_Battle.setText("Battles: " + m_BattleWin + "/" + m_BattleTotal);
        
        
        if(m_BtnChallenge != null )
        {
	        if(  (IsMe(m_FBID) || m_IsToHideButtonChallenge) )
	        {
	        	m_BtnChallenge.setVisibility(View.GONE);
	        }
	        
	        else 
	        {
	        	m_BtnChallenge.setOnClickListener(getOnClickChallenge());
	        	
	        }
        }
        
    }
    
    public String GetFBID()
    {
    	return m_FBID;
    }
    
    public String GetName()
    {
    	return m_Name;
    }
    
    private View.OnClickListener getOnClickChallenge()
    {
    	return new View.OnClickListener() 
        {
            @Override
            public void onClick(View view)
            {
            	//String url = "fb://photo/" +  m_PhotoID2;
         		//Intent i = new Intent(Intent.ACTION_VIEW);
         		//i.setData(Uri.parse(url));
         		//m_Activity.startActivity(i);
            	//m_CounterAttackParseObject = m_ParseObj;
            	OnClickChallenge();
            }
        };
    }
    
    private void OnClickChallenge()
    {
    	 AlertDialog.Builder builder = new AlertDialog.Builder(m_Activity);
         CharSequence camera = m_Activity.getResources().getString(R.string.action_photo_camera);
         CharSequence gallery = m_Activity.getResources().getString(R.string.action_photo_gallery);
         builder.setCancelable(true).
                 setItems(new CharSequence[] {camera, gallery}, new DialogInterface.OnClickListener()
                 {
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i) 
                     {
                    	 
                         if (i == CAMERA) 
                         {
                             startCameraActivity();
                         } 
                         else if (i == GALLERY) 
                         {
                             startGalleryActivity();
                         }
                     }
                 });
         builder.show();
    }
    
    private void startCameraActivity() 
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        tempUri = getTempUri();
        if (tempUri != null) 
        {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
        }
        if(m_HostFragment != null)
        {
        	m_ToAttackObject = this;
        	m_HostFragment.startActivityForResult(intent, REQUEST_CAMERA);
        }
    }

    private void startGalleryActivity() 
    {
        tempUri = null;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        String selectPicture = m_Activity.getResources().getString(R.string.select_picture);
        
        if(m_HostFragment != null)
        {
        	m_ToAttackObject = this;
        	m_HostFragment.startActivityForResult(Intent.createChooser(intent, selectPicture), REQUEST_PHOTO);
        }
    }
    
    public static Uri GetResultURI()
    {
    	return tempUri;
    }
    
    private Uri getTempUri()
    {
        String imgFileName = FILE_PREFIX + System.currentTimeMillis() + FILE_SUFFIX;

        // Note: on an emulator, you might need to create the "Pictures" directory in /mnt/sdcard first
        //       % adb shell
        //       % mkdir /mnt/sdcard/Pictures
        File image = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imgFileName);
        return Uri.fromFile(image);
    }
   

    
    private boolean IsMe(String ThisID)
    {
    	ParseUser PU = ParseUser.getCurrentUser();    	
    	if(PU != null)
    	{
    		String MyID = PU.getString("FBID");
    		if(MyID != null && MyID.isEmpty() == false)
    		{
    			if(MyID.equalsIgnoreCase(ThisID))
    				return true;
    		}
    	}
    	
    	return false;
    }

    
    public void setAdapter(BaseAdapter padapter) 
    {
        this.adapter = padapter;
    }
    
    public BaseAdapter getAdapter() 
    {
        return this.adapter;
    }
       
     
    protected void notifyDataChanged() 
    {
    	if(adapter != null)
    		adapter.notifyDataSetChanged();
    }
    
    
}
